<template>
  <div class="goods">
    <div class="goods_header">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-card shadow="hover" class="goods_con">
      <!-- 搜索框 -->
      <div class="goods_sea">
        <el-input placeholder="请输入内容" clearable @clear="getGoodsList" v-model="queryinfo.query">
          <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
        </el-input>
        <el-button type="primary" @click="goodsAdd">添加商品</el-button>
      </div>
      <!-- 列表 -->
      <el-table :data="goodsList" style="width: 100%">
        <el-table-column label="#" width="60" type="index">
        </el-table-column>
        <el-table-column label="商品名称" width="700" prop="goods_name">
        </el-table-column>
        <el-table-column label="商品价格(元)" width="90" prop="goods_price">
        </el-table-column>
        <el-table-column label="商品重量" width="90" prop="goods_weight">
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{gettime(scope.row.add_time)}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"
              @click="handleEdit(scope.row)"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete"
              @click="handleDelete(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block paging">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="queryinfo.pagenum" :page-sizes="[5,8,15]" :page-size="queryinfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
      <!-- 编辑框 -->
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'GooDs',
  data() {
    return {
      //请求商品列表数据
      queryinfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      // 请求商品列表总数
      total: 0,
      // 商品列表数据
      goodsList: [],
      // 添加框的显示
      dialogAddVisible: false,
      // 添加框的表单验证效果
      AddRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' },
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { min: 15, max: 20, message: '长度在 15 到 20 个字符', trigger: 'blur' },
        ],
        mobile: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { min: 11, max: 11, message: '长度11个数字', trigger: 'blur' },
        ],
      },
    }
  },
  computed: {
    // 时间戳转换日期
    gettime() {
      return (time) => {
        const date = new Date(time * 1000)
        const Y = date.getFullYear() + '-'
        const M =
          (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        const D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' '
        const H = date.getHours() + ':'
        const M2 = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        const S = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
        return Y + M + D + H + M2 + S
      }
    },
  },
  methods: {
    async getGoodsList() {
      const { data: Goodsres } = await this.$http.get('goods', { params: this.queryinfo })
      this.goodsList = Goodsres.data.goods
      this.total = Goodsres.data.total
      console.log(Goodsres)
    },
    // 分页每页数据条数发生变化
    handleSizeChange(val) {
      this.queryinfo.pagesize = val
      this.getGoodsList()
    },
    // 分页数据页数发生变化
    handleCurrentChange(val) {
      this.queryinfo.pagenum = val
      this.getGoodsList()
    },
    // 确认关闭添加框、修改框
    handleClose(done) {
      this.$confirm('确认关闭?填写数据将不被保存')
        .then(() => {
          done()
        })
        .catch(() => {})
    },
    // 点击添加
    goodsAdd() {
      this.$router.push('/goods/add')
    },
    // 关闭添加框
    addcancel() {
      this.$refs.reference.resetFields()
    },
    //  点击编辑
    async handleEdit() {
      // const { data: askres } = await this.$http.get(`goods/${row.goods_id}`)
      // // console.log(askres)
      // const { data: editRes } = await this.$http.put(`goods/${row.goods_id}`, {
      //   goods_name: row.goods_name,
      //   goods_price: row.goods_price,
      //   goods_number: row.goods_number,
      //   goods_weight: row.goods_weight,
      //   goods_introduce: askres.data.goods_introduce,
      //   pics: askres.data.pics,
      //   attrs: askres.data.attrs,
      // })
      // console.log(editRes)
    },
    // 点击删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除此分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const { data: deleteres } = await this.$http.delete(`goods/${row.goods_id}`)
          if (deleteres.meta.status != 200) {
            this.$message.error('删除失败，请重试')
          }
          this.$message.success('删除成功')
          this.getGoodsList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
  },
  created() {
    this.getGoodsList()
  },
}
</script>

<style lang='less' scoped>
.goods_header {
  .el-breadcrumb {
    font-size: 17px;
  }
}
.goods_con {
  margin-top: 30px;
  height: 90%;
  background: #fff;
  padding: 20px;
}
.goods_sea {
  width: 560px;
  display: flex;
  justify-content: space-between;
  .el-input {
    width: 80%;
  }
}
.el-table {
  margin-top: 20px;
}
.paging {
  margin-top: 20px;
}
</style>